package com.liy.supercloud.ssoserver.service.impl;

import com.liy.cloud.system.vo.SysPermissionVO;
import com.liy.framework.common.extvo.ResultVO;
import com.liy.framework.common.redis.utils.RedisKeyUtils;
import com.liy.framework.common.redis.utils.RedisUtils;
import com.liy.framework.common.utils.JSONUtils;
import com.liy.supercloud.ssoserver.feign.SystemFeignClient;
import com.liy.supercloud.ssoserver.service.RolePermissionService;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RolePermissionServiceImpl implements RolePermissionService {

    @Autowired
    private RedisUtils redisUtils;

    /**
    *  去查 sys 服务
    */
    @Autowired
    private SystemFeignClient systemFeignClient;


    /**
     * 查询 角色 权限
     */
    public Map<Object, Object> findRolesPermission(String[] roles) {
        Map<Object, Object> map = new HashMap();
        String[] var3 = roles;
        int var4 = roles.length;

        for(int var5 = 0; var5 < var4; ++var5) {
            String roleId = var3[var5];
            map.putAll(this.redisUtils.hmget(RedisKeyUtils.getRolePermissionWebKey(roleId)));
        }

        return map;
    }


    public Map<Object, Object> findSharePermission(String sysId) {
        Map<Object, Object> map = this.redisUtils.hmget("PER:COMMW");
        return map;
    }


    /**
    *  同步共享到权限到 redis
    **/
    public void synSharePermissionToRedis(String sysId) throws Exception {
        ResultVO result = this.systemFeignClient.querySharedPermission(sysId);
        String json = JSONUtils.object2Json(result.getData());
        List<SysPermissionVO> sharePermissionList = (List<SysPermissionVO>) JSONUtils.json2List(json, new TypeReference<List<SysPermissionVO>>() {});
        Map<String, Object> permissionMaps = new HashMap();
        Map<String, Object> permissionWebMaps = new HashMap();
        Iterator var7 = sharePermissionList.iterator();

        while(var7.hasNext()) {
            SysPermissionVO permissionVO = (SysPermissionVO)var7.next();
            String[] urls = permissionVO.getUrl().split(",");
            String pre = permissionVO.getSysId().substring(permissionVO.getSysId().indexOf("-") + 1, permissionVO.getSysId().length());
            String[] var11 = urls;
            int var12 = urls.length;

            for(int var13 = 0; var13 < var12; ++var13) {
                String url = var11[var13];
                permissionMaps.put("/" + pre + url, permissionVO.getSysId());
            }

            if (!"N".equals(permissionVO.getCode())) {
                permissionWebMaps.put(permissionVO.getCode().replace(".", "_"), permissionVO.getUrl());
            }
        }

        this.redisUtils.hmset("PER:COMM", permissionMaps);
        this.redisUtils.hmset("PER:COMMW", permissionWebMaps);
    }


    /**
     *  同步共享 角色-权限 到 redis
     **/
    public void synRolePermissionToRedis() throws Exception {
        Map<String, Object> permissionMaps = new HashMap();
        Map<String, Object> permissionWebMaps = new HashMap();
        ResultVO result = this.systemFeignClient.queryRolePermission("all");
        String json = JSONUtils.object2Json(result.getData());
        List<SysPermissionVO> rolePermissionList = (List<SysPermissionVO>) JSONUtils.json2List(json, new TypeReference<List<SysPermissionVO>>() {
        });
        String curRoleId = "";
        Iterator var7 = rolePermissionList.iterator();

        while(var7.hasNext()) {
            SysPermissionVO permissionVO = (SysPermissionVO)var7.next();
            if ("".equals(curRoleId)) {
                curRoleId = permissionVO.getRoleId();
            }

            if (!curRoleId.equals(permissionVO.getRoleId())) {
                this.redisUtils.hmset(RedisKeyUtils.getRolePermissionKey(curRoleId), permissionMaps);
                this.redisUtils.hmset(RedisKeyUtils.getRolePermissionWebKey(curRoleId), permissionWebMaps);
                curRoleId = permissionVO.getRoleId();
                permissionMaps = new HashMap();
                permissionWebMaps = new HashMap();
            }

            String[] urls = permissionVO.getUrl().split(",");
            String pre = permissionVO.getSysId().substring(permissionVO.getSysId().indexOf("-") + 1, permissionVO.getSysId().length());
            String[] var11 = urls;
            int var12 = urls.length;

            for(int var13 = 0; var13 < var12; ++var13) {
                String url = var11[var13];
                permissionMaps.put("/" + pre + url, permissionVO.getModelName() + "." + permissionVO.getName());
            }

            permissionWebMaps.put(permissionVO.getCode().replace(".", "_"), permissionVO.getUrl());
        }

        if (!"".equals(curRoleId)) {
            this.redisUtils.hmset(RedisKeyUtils.getRolePermissionKey(curRoleId), permissionMaps);
            this.redisUtils.hmset(RedisKeyUtils.getRolePermissionWebKey(curRoleId), permissionWebMaps);
        }

    }


    /**
     *  同步共享 角色-权限 到 redis
     **/
    public void synRolePermissionToRedis(String roleId) throws Exception {
        Map<String, Object> permissionMaps = new HashMap();
        Map<String, Object> permissionWebMaps = new HashMap();
        ResultVO result = this.systemFeignClient.queryRolePermission(roleId);
        String json = JSONUtils.object2Json(result.getData());
        List<SysPermissionVO> rolePermissionList = (List<SysPermissionVO>) JSONUtils.json2List(json, new TypeReference<List<SysPermissionVO>>() {});
        Iterator var7 = rolePermissionList.iterator();

        while(var7.hasNext()) {
            SysPermissionVO permissionVO = (SysPermissionVO)var7.next();
            String[] urls = permissionVO.getUrl().split(",");
            String pre = permissionVO.getSysId().substring(permissionVO.getSysId().indexOf("-") + 1, permissionVO.getSysId().length());
            String[] var11 = urls;
            int var12 = urls.length;

            for(int var13 = 0; var13 < var12; ++var13) {
                String url = var11[var13];
                permissionMaps.put("/" + pre + url, permissionVO.getModelName() + "." + permissionVO.getName());
            }

            permissionWebMaps.put(permissionVO.getCode().replace(".", "_"), permissionVO.getUrl());
        }

        this.redisUtils.hmset(RedisKeyUtils.getRolePermissionKey(roleId), permissionMaps);
        this.redisUtils.hmset(RedisKeyUtils.getRolePermissionWebKey(roleId), permissionWebMaps);
    }
}
